package com.nwpu.bigdata.controller;

import com.nwpu.bigdata.dto.BackendResult;
import com.nwpu.bigdata.model.FaultExtend;
import com.nwpu.bigdata.model.FaultReasonExtend;
import com.nwpu.bigdata.model.User;
import com.nwpu.bigdata.service.DiagnosisService;
import com.nwpu.bigdata.service.FaultDetectionService;
import org.apache.commons.lang.builder.ReflectionToStringBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author carlin
 * @date 2018/8/2 0002 16:49
 */
@RestController
@RequestMapping("/fault")
//@SessionAttributes("currentUser")
public class FaultController {

    private static Logger logger = LoggerFactory.getLogger(LogController.class);

    private FaultDetectionService faultDetectionService;

    private DiagnosisService diagnosisService;

    private User user;

    @Autowired
    public FaultController(FaultDetectionService faultDetectionService, DiagnosisService diagnosisService) {
        this.faultDetectionService = faultDetectionService;
        this.diagnosisService = diagnosisService;
        this.user = new User();
        user.setId(1L);
        user.setUsername("admin");
        user.setEmail("grcheung@163.com");
        user.setUserPass("123456");
    }

    @GetMapping(value = "/list/{page}", consumes = "*/*", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public BackendResult<List<FaultExtend>> listFaults(@PathVariable Long page/*,
                                                       @ModelAttribute("currentUser") User user*/) {
        BackendResult<List<FaultExtend>> backendResult = faultDetectionService.listFaults(user, page);
        logger.debug("faults is :{}", ReflectionToStringBuilder.toString(backendResult));
        return backendResult;
    }

    @GetMapping(value = "/list/count", consumes = "*/*", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public BackendResult<Long> countFault(/*@ModelAttribute("currentUser") User user*/) {
        return new BackendResult<>(true, faultDetectionService.countFault(user));
    }

    /**
     * 故障若为确认，需要对其进行确认，否则前台不可以点击
     *
     * @param id
     * @return 确认状态信息 更新成功为true
     */
    @PostMapping(value = "/list/{id}/status/ack/update",
            consumes = "*/*",
            produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public BackendResult<Integer> updateFaultStatus(@PathVariable Long id) {
        return faultDetectionService.updateFaultStatusAck(id);
    }

    @GetMapping(value = "/list/{id}/diag",
            consumes = "*/*",
            produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public BackendResult<FaultReasonExtend> getFaultDiagInfo(
            @PathVariable("id") Long faultId) {
            return diagnosisService.getFaultReason(user, faultId);
    }

    @PostMapping(value = "/list/{id}/reason/insertion",
            consumes = "*/*",
            produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public BackendResult<Boolean> insertFaultReasonInfo(
            @PathVariable("id") Long faultId,
            @RequestParam(value = "reason", required = true) String reason) {
        BackendResult<Boolean> backendResult = diagnosisService.setFaultReason(faultId, reason);
        if (backendResult.isSuccess()) {
            faultDetectionService.updateFaultStatusAck(faultId);
            return backendResult;
        }else {
            return backendResult;
        }
    }

}
